<template>
  <div id="home">
    <nav-bar class="home-nav">
      <template v-slot:center> 购物车 </template>
    </nav-bar>

    <scroll class="contents" ref="scroll" :probe-type="0" :pull-upLoad="true" @scroll="contentScroll" @pullingUp="loadMore">
      <home-swiper :banners="banners" />
      <home-recommend-view :recommends="recommends"/>
      <home-feature/>
      <tab-control class="tab-control" 
                    :titles="['流行', '新款', '精选']" 
                    @tabClick="tabClick" />
      <goods-list :goods="showGoods"/>
    </scroll>

    <back-top v-show="isshowBackTop" @click.native="backClick" />

  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar.vue";
import Scroll from 'components/common/scroll/Scroll.vue';

import HomeSwiper from './childComps/HomeSwiper.vue';
import HomeRecommendView from './childComps/HomeRecommendView.vue';
import HomeFeature from './childComps/HomeFeature.vue';
import TabControl from 'components/content/tabControl/TabControl.vue';
import BackTop from 'components/content/backTop/BackTop.vue';

import GoodsList from 'components/content/goods/GoodsList.vue';

import { 
  getHomeMultidata,
  getHomeGoods
 } from "network/home.js";

export default {
  components: { NavBar, HomeSwiper, HomeRecommendView, HomeFeature, TabControl, GoodsList, Scroll, BackTop },
  data() {
    return {
      banners: [],
      recommends: [],
      titles: ['流行', '新款', '精选'],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []}
      },
      currentType: 'pop',
      isshowBackTop: false
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  created() {
    this.getHomeMultidata();
    this.getHomeGoods('pop');
    this.getHomeGoods('new');
    this.getHomeGoods('sell');
  },
  mounted() {
    this.$bus.on('itemImageLoad', () => {
      this.$refs.scroll.refresh()
    })
  },
  methods: {
    debounce(func, delay){
      let timer = null

      return function (...args) {
        if (timer) clearTimeout(timer)

        timer = setTimeout(() => {
          func.apply(this. args)
        }, delay)
      }
    },
    //网络请求方法
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1
        this.$refs.scroll.finishPullUp()
      })
    },
    //事件监听事件
    tabClick(index){
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0)
    },
    contentScroll(position) {
      this.isshowBackTop = (-position.y) > 850
    },
    loadMore() {
      this.getHomeGoods(this.currentType)
    }
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9;
}
.tab-control {
  position: sticky;
  top: 44px;
  z-index: 2;
}
.contents {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>